package com.neusoft.student.controller;

import com.neusoft.student.model.Student;
import com.neusoft.student.service.StudentService;
import com.neusoft.student.service.impl.StudentServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Author: duan
 * @Date: Create in 15:03 2020/10/22
 */
@WebServlet("/studentadd")
public class StudentAddServlet extends HttpServlet {
    private StudentService studentService = new StudentServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("StudentAddServlet:::doPost");
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String sex = req.getParameter("sex");
        String phone = req.getParameter("phone");
        String dormitory = req.getParameter("dormitory");

        boolean insert = studentService.insert(new Student(0, username, password, sex, phone, dormitory));
        if (insert) {
            req.getRequestDispatcher("/studentlist").forward(req, resp);
        } else {
            req.setAttribute("msg", "添加失败");
            req.getRequestDispatcher("/student_add.jsp").forward(req, resp);
        }
    }
}
